import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { MessageEntity } from 'src/Entity/message.entity';

@Injectable()
export class MessageService {
  async findAll(): Promise<MessageEntity[]> {
    return await MessageEntity.find();
  }

  async createOrderMessage(
    hotelId: number,
    orderId: number,
    content: string,
  ): Promise<MessageEntity> {
    const message = new MessageEntity();
    message.content = content;
    message.bookingOrderId = orderId;
    message.hotelId = hotelId;
    message.confirmRequired = 0;
    message.confirmRead = 0;
    message.messageChannel = 4;
    return await MessageEntity.save(message);
  }

  async createRoomMessage(
    hotelId: number,
    roomId: number,
    content: string,
  ): Promise<MessageEntity> {
    const message = new MessageEntity();
    message.content = content;
    message.roomId = roomId;
    message.hotelId = hotelId;
    message.confirmRequired = 0;
    message.confirmRead = 0;
    message.messageChannel = 2;
    return await MessageEntity.save(message);
  }

  async createSystemMessage(
    hotelId: number,
    content: string,
  ): Promise<MessageEntity> {
    const message = new MessageEntity();
    message.content = content;
    message.hotelId = hotelId;
    message.confirmRequired = 1;
    message.confirmRead = 1;
    message.messageChannel = 0;
    return await MessageEntity.save(message);
  }

  async createHotelMessage(
    hotelId: number,
    content: string,
    confirmRequired: number,
  ): Promise<MessageEntity> {
    const message = new MessageEntity();
    message.content = content;
    message.hotelId = hotelId;
    message.confirmRequired = confirmRequired;
    message.confirmRead = 1;
    message.messageChannel = 1;
    return await MessageEntity.save(message);
  }

  async confirmRequire(userId: number, messageId: number) {
    const message = await MessageEntity.findOne({
      where: { id: messageId, isDelete: 0 },
    });
    if (!message) {
      throw new HttpException(
        {
          message: 'This message is not exist !',
        },
        HttpStatus.BAD_REQUEST,
      );
    }
    const date = new Date();
    message.confirmRequired = 1;
    message.confirmUserId = userId;
    message.confirmTime = date.toString();
    message.callbackContent = '回调函数';

    const updateMessage = await MessageEntity.save(message);
    return updateMessage.callbackContent;
  }

  async confirmRead(userId: number, messageId: number) {
    const message = await MessageEntity.findOne({
      where: { id: messageId, isDelete: 0 },
    });
    if (!message) {
      throw new HttpException(
        {
          message: 'This message is not exist !',
          error: 'menuName must be unique',
        },
        HttpStatus.BAD_REQUEST,
      );
    }
    if (message.firstReadUserId != 0) {
      return '消息已读';
    }
    message.confirmRead = 1;
    const date = new Date();
    message.firstReadUserId = userId;
    message.readTime = date.toString();
    message.callbackContent = '消息已读';
    const updateMessage = await MessageEntity.save(message);

    return updateMessage.callbackContent;
  }

  async deleteMessage(messageId: number) {
    const message = await MessageEntity.findOne({ where: { id: messageId } });
    if (!message) {
      throw new HttpException(
        {
          message: 'This message is not exist !',
          error: 'menuName must be unique',
        },
        HttpStatus.BAD_REQUEST,
      );
    }

    message.isDelete = 1;
    await MessageEntity.save(message);

    return 'true';
  }

  async findMessage(
    channel: number,
    pageNo: number,
    pageSize: number,
  ): Promise<MessageEntity[]> {
    const offset = (pageNo - 1) * pageSize;

    const message = await MessageEntity.createQueryBuilder('message')
      .offset(offset)
      .limit(pageSize)
      .where(
        'message.messageChannel = :messageChannel and message.isDelete=:isDelete',
        { messageChannel: channel, isDelete: 0 },
      )
      .getMany();

    return message;
  }

  async findMessageByRoomId(roomId: number, pageNo: number, pageSize: number) {
    const offset = (pageNo - 1) * pageSize;
    const message = await MessageEntity.createQueryBuilder('message')
      .offset(offset)
      .limit(pageSize)
      .where('message.roomId = :roomId and message.isDelete=:isDelete', {
        roomId: roomId,
        isDelete: 0,
      })
      .getMany();

    return message;
  }
}
